import Vue from 'vue';
import store from '@/store';
import vueTheme from 'theme-vue';
import VueRouter from 'vue-router';
import adminRoute from './adminRoute';
import frontRoute from './frontRoute';
import staticRouter from './staticRouter';
import dynamicRouter from './dynamicRouter';
const VueRouterPush = VueRouter.prototype.push;
const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return VueRouterPush.call(this, location, onResolve, onReject)
	return VueRouterPush.call(this, location).catch(err => err)
}
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return VueRouterReplace.call(this, location, onResolve, onReject)
	return VueRouterReplace.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const router = new VueRouter({
	routes: [...staticRouter, ...adminRoute, ...frontRoute]
})
router.beforeEach((to, from, next) => {
	let Theme = new vueTheme();
	let token = Vue.prototype.$local.get('token');
	let siteInfo = Vue.prototype.$local.get('siteInfo');
	let userInfo = Vue.prototype.$local.get('userInfo');
	let isLogin = !!token && !!userInfo;
	if (to.matched.some(n => n.name === 'Front')) {
		Theme.changeTheme('#600807');
	} else if (to.matched.some(n => n.name === 'Admin')) {
		Theme.changeTheme('#0C61B5');
	}
	if (!isLogin && to.name !== 'Login') {
		next({
			path: '/login'
		});
		return false;
	}
	if (!to.matched.length) {
		next({
			path: '/404'
		});
		return false;
	}
	next();
})
export default router